<template>
  <!-- 采购/销售统计 -->
  <div class="allBox">
    <h1>销售/采购数据</h1>
    <div class="allContent">
      <div v-if="list.length > 0" id="salesProcurement" class="allWidthHeight" />
      <div v-else class="kong">暂无数据</div>
    </div>
  </div>
</template>
<script>
import * as echarts from 'echarts'
export default {
  props: {},
  data() {
    return {
      myChart: null,
      list: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    infoSize() {
      this.myChart.resize()
    },
    /* 获取采购/销售统计 */
    async getSalesProcurement() {
      const res = await this.$axios.get('/board/composite/purchaseSale')
      if (res.data.code === 200) {
        this.list = res.data.data
        let data = {
          months: [],
          xnum: [],
          cnum: []
        }
        setTimeout(async () => {
          res.data.data.forEach((item) => {
            item.monthDay = item.monthDay.slice(5, 10)
            data.months.push(item.monthDay)
            data.cnum.push(item.purchasePrice)
            data.xnum.push(item.salePrice)
          })
          this.setSalesProcurement(data)
        }, 500)
      }
    },
    /* 采购/销售统计echarts */
    setSalesProcurement(data) {
      const option = {
        grid: {
          left: '18%', // 距离左侧的距离
          right: '10%', // 距离右侧的距离
          top: '35%', // 距离顶部的距离
          bottom: '20%' // 距离底部的距离
        },
        legend: {
          data: ['原料采购(万元)', '销售收入(万元)'],
          right: '20px',
          top: '10px',
          width: '100px',
          textStyle: {
            color: '#fff' // 这里设置图例的字体颜色为红色
          },
          itemWidth: 10, // 设置图例标记的图形宽度
          itemHeight: 10 // 设置图例标记的图形高度
        },
        xAxis: [
          {
            splitLine: { show: false },
            axisPointer: {
              type: 'shadow'
            },
            axisLine: {
              // 坐标轴 轴线
              lineStyle: {
                color: '#fff',
                width: 1,
                type: 'solid'
              }
            },
            axisLabel: {
              color: '#fff',
              formatter: '{value}月',
              interval: 0 // 代表显示所有x轴标签显示
            },
            type: 'category',
            data: data.months
          }
        ],
        yAxis: [
          {
            splitLine: { show: false },
            axisLine: {
              // 坐标轴 轴线
              lineStyle: {
                color: '#fff',
                width: 1,
                type: 'solid'
              }
            },
            axisLabel: {
              color: '#fff'
            },
            axisTick: {
              show: false
            },
            type: 'value'
          }
        ],
        dataZoom: [
          {
            type: 'inside'
          }
        ],
        series: [
          {
            name: '原料采购(万元)',
            type: 'bar',
            data: data.cnum
          },
          {
            name: '销售收入(万元)',
            type: 'bar',
            data: data.xnum
          }
        ]
      }
      if (
        this.myChart != null &&
        this.myChart !== '' &&
        this.myChart !== undefined
      ) {
        this.myChart.dispose() // 销毁
      }
      this.myChart = echarts.init(document.getElementById('salesProcurement'))
      this.myChart.clear()
      this.myChart.setOption(option, true)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
